export default {
    data() {
        return {
            // base_url: process.env.NODE_ENV === 'development' ? 'http://frp.51zhebao.com:7080' : 'https://51zhebao.com',
            // base_url: 'https://todo.51zhebao.com',
            base_url: process.env.NODE_ENV === 'development' ? 'http://127.0.0.1:9401' : 'https://todo.51zhebao.com',

            try_times: 0, // 尝试授权次数
        }
    },

    computed: {
        token() {
            return this.$store.state.token
        }
    },

    methods: {
        request(options) {

            options = options || {}

            const vm = this

            return new Promise((resolve, reject) => {

                let _options = {

                    method: 'POST',

                    header: {
                        Authorization: 'JWT ' + vm.token // token是全局的
                    },

                    sslVerify: process.env.NODE_ENV === 'development' ? false : true,

                    success: function(res) {
                        switch (res.statusCode) {
                            case 401:
                                if (vm.try_times > 10) return; // todo: 提示授权失败

                                vm.try_times++;
                                this.weixinLogin().then(() => this.request(options).then(data=> resolve.bind(vm)(data)));
                                break;
                            default:
                                vm.try_times = 0;
                                resolve.bind(vm)(res.data);
                        }

                    }.bind(vm),

                    fail: function() {
                        console.log(arguments);
                        reject.bind(vm)(arguments)
                    }.bind(vm)
                }

                _options = Object.assign(_options, options);

                uni.request(_options)
            })
        },

        get(url, data) {
            const options = {
                url: this.base_url + '/api2' + url,
                method: 'GET',
                data
            }

            return this.request(options);
        },

        post(url, data) {
            const options = {
                url: this.base_url + '/api2' + url,
                method: 'POST',
                data
            }

            return this.request(options);

        },

        put(url, data) {
            const options = {
                url: this.base_url + '/api2' + url,
                method: 'PUT',
                data
            }

            return this.request(options);
        },

        delete(url, data) {
            const options = {
                url: this.base_url + '/api2' + url,
                method: 'DELETE',
                data
            }

            return this.request(options);
        },

        // 小程序登陆
        weixinLogin() {
            const vm = this;
            return new Promise((resolve, reject) => {
                vm.getWeixinCode().then(code => {
                    vm.requestToken(code).then(() => resolve());
                })
            });
        },

        getWeixinCode() {
            const vm = this;

            return new Promise((resolve, reject) => {

                const options = {
                    provider: 'weixin',
                    success(loginResult) {
                        resolve(loginResult.code);
                    }
                };

                uni.login(options);

            });
        },


        requestToken(code) {
            const vm = this;
            return new Promise((resolve, reject) => {

                const options = {

                    url: `${this.base_url}/weixin/mini/login`,

                    data: {
                        code: code
                    },

                    sslVerify: process.env.NODE_ENV === 'development' ? false : true,

                    method: 'POST',

                    success: function(response) {
                        const token = response.data.token;
                        this.$store.dispatch('store_token', token);
                        resolve();
                    }.bind(vm)
                };

                uni.request(options)

            });
        },
    },

    onLoad(){
        console.log('onload')
    }
}
